package com.funmi.lizip.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.funmi.lizip.common.base.R;
import com.funmi.lizip.common.base.ResultCode;
import com.funmi.lizip.common.exception.ApplicationException;
import com.funmi.lizip.entity.TaskDelivery;
import com.funmi.lizip.entity.TaskFill;
import com.funmi.lizip.entity.dto.task.TaskQueryRequest;
import com.funmi.lizip.entity.dto.taskDelivery.TaskDeliveryAddRequest;
import com.funmi.lizip.entity.dto.taskDelivery.TaskDeliveryQueryRequest;
import com.funmi.lizip.entity.dto.taskDelivery.TaskDeliveryUpdateRequest;
import com.funmi.lizip.entity.dto.taskFill.TaskFillAddRequest;
import com.funmi.lizip.entity.dto.taskFill.TaskFillQueryRequest;
import com.funmi.lizip.entity.dto.taskFill.TaskFillUpdateRequest;
import com.funmi.lizip.entity.vo.TaskDeliveryVo;
import com.funmi.lizip.entity.vo.TaskFillVo;
import com.funmi.lizip.entity.vo.TaskVo;
import com.funmi.lizip.service.TaskFillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@Api(tags = "数据采集和核查")
@RequestMapping("taskFill")
public class TaskFillController {

    @Autowired
    private TaskFillService  taskFillService;

    @ApiOperation(value = "任务填写的新增")
    @PostMapping("")
    public R addTaskFill(@RequestBody TaskFillAddRequest request){
        if(request==null){
            throw new ApplicationException(ResultCode.NOT_FOUND);
        }

        request.setCreateAt(LocalDateTime.now());
        request.setIsDelete(false);
        TaskFill taskFill = new TaskFill();
        BeanUtils.copyProperties(request,taskFill);
        boolean save = taskFillService.save(taskFill);

        if (save) {
            return R.success().message("新增成功");
        } else {
            throw new ApplicationException("新增失败");
        }
    }

    @ApiOperation(value = "任务填写的修改")
    @PutMapping("/{id}")
    public R updateTaskFill(@RequestBody(required = false) TaskFillUpdateRequest request){
        if(request==null||request.getId()==null){
            throw new ApplicationException(ResultCode.NOT_FOUND);
        }
        TaskFill taskFill = new TaskFill();
        BeanUtils.copyProperties(request,taskFill);

        boolean update = taskFillService.updateById(taskFill);
        if (update) {
            return R.success().message("修改成功");
        } else {
            throw new ApplicationException("修改失败");
        }
    }

    @ApiOperation(value = "任务填写的删除")
    @DeleteMapping("/{id}")
    public R deleteTaskFill(@PathVariable Integer id){
        if (id <= 0) {
            throw new ApplicationException(ResultCode.FILE_DELETE_ERROR);
        }
        boolean result = taskFillService.removeById(id);
        if (result) {
            return R.success().message("删除成功");
        } else {
            return R.fail().message("删除失败");
        }
    }

    @ApiOperation("根据任务填写输入进行检索")
    @GetMapping("")
    public R listPage(TaskFillQueryRequest request){
        long current = 1;
        long size = 5;
        TaskFill taskFillQuery = new TaskFill();
        if(request!=null){
            BeanUtils.copyProperties(request,taskFillQuery);
            current =request.getPage();
            size =request.getPageSize();
        }

        QueryWrapper<TaskFill> qw =new QueryWrapper<>(taskFillQuery);
        qw.select().orderByDesc("create_at");
        Page<TaskFill> taskFillPage = taskFillService.page(new Page<>(current, size), qw);

        PageDTO<TaskFillVo> taskFillVoPage = new PageDTO<>(taskFillPage.getCurrent(), taskFillPage.getSize(), taskFillPage.getTotal());

        List<TaskFillVo> taskFillVoList = taskFillPage.getRecords().stream().map(taskFill -> {
            TaskFillVo taskFillVo = new TaskFillVo();
            BeanUtils.copyProperties(taskFill, taskFillVo);
            return taskFillVo;
        }).collect(Collectors.toList());
        taskFillVoPage.setRecords(taskFillVoList);
        return R.success().data(taskFillVoPage);
    }


    @ApiOperation("根据ID获取任务填写详情")
    @GetMapping("/{id}")
    public R listById(TaskFillQueryRequest request){
        if(request==null ){
            throw new ApplicationException(ResultCode.NOT_FOUND);
        }
        TaskFill taskFill = taskFillService.listById(request);
        return  R.success().data(taskFill);
    }
}
